
package com.scj.ho;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BrowerPicActivity extends Activity {

    private String current_name = null;

    private Button share = null;

    private Button back = null;

    private int currentImageId = 0;

    private Gallery g;

    private final Integer[] mImageIds_year = {
            R.drawable.new_year_01, R.drawable.new_year_02, R.drawable.new_year_03,
            R.drawable.new_year_04, R.drawable.new_year_05, R.drawable.new_year_06

    };

    private final Integer[] mImageIds_qingren = {
            R.drawable.qingren_1, R.drawable.qingren_2, R.drawable.qingren_3, R.drawable.qingren_4,
            R.drawable.qingren_5, R.drawable.qingren_6, R.drawable.qingren_7, R.drawable.qingren_8,
            R.drawable.qingren_9, R.drawable.qingren_10, R.drawable.qingren_11,
            R.drawable.qingren_12, R.drawable.qingren_13

    };

    private final Integer[] mImageIds_yuanxiao = {
            R.drawable.yuanxiao_1, R.drawable.yuanxiao_2, R.drawable.yuanxiao_3
    };// png

    // private void initImage()
    // {
    // HashMap<Integer,String>
    // }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brower);
        current_name = getIntent().getStringExtra(MyConstant.KEY);
        share = (Button) findViewById(R.id.button_send);
        back = (Button) findViewById(R.id.button_back);

        // Reference the Gallery view
        g = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        Integer[] currentImage = mImageIds_year;
        if (getString(R.string.qingrenjie_name).equals(current_name)) {
            currentImage = mImageIds_qingren;

        } else if (getString(R.string.yuanxiaojie_name).equals(current_name)) {
            currentImage = mImageIds_yuanxiao;
        }

        g.setAdapter(new ImageAdapter(this, currentImage));

        // Set a item click listener, and just Toast the clicked position
        // g.setOnItemClickListener(new OnItemClickListener() {
        // public void onItemClick(AdapterView<?> parent, View v, int position,
        // long id) {
        // Toast.makeText(BrowerPicActivity.this, "" + position,
        // Toast.LENGTH_SHORT).show();
        // }
        // });

        g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Object object = parent.getAdapter().getItem(position);
                if (object != null) {
                    currentImageId = Integer.valueOf("" + object);
                }

            }

            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

        // We also want to show context menu for longpressed items in the
        // gallery
        registerForContextMenu(g);

        share.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                share();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                BrowerPicActivity.this.onBackPressed();
            }
        });

    }

    private void share() {
        // String packName = getApplication().getPackageName();
        // Uri path = Uri.parse("android.resource://" + packName + "/" +
        // currentImageId);
        String fileName = currentImageId + "temporary_file.jpg";

        Resources r = getResources();
        BitmapDrawable bmpDraw = new BitmapDrawable(r.openRawResource(currentImageId));
        Bitmap bmp = bmpDraw.getBitmap();
        //
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File f1 = new File(Environment.getExternalStorageDirectory() + File.separator + "scjho");

        if (!f1.exists()) {
            f1.mkdirs();
        }

        // File f =

        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "scjho"
                + File.separator + fileName);
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            out.write(bytes.toByteArray());
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                if (file.exists()) {
                    Log.i("test", "1111111111111111111");
                }
                Uri path = Uri.fromFile(file);
                HolidayUtil.shareText(this, path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // @Override
    // public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
    // menuInfo) {
    // // menu.add("test");
    // }

    // @Override
    // public boolean onContextItemSelected(MenuItem item) {
    // AdapterContextMenuInfo info = (AdapterContextMenuInfo)
    // item.getMenuInfo();
    // Toast.makeText(this, "Longpress: " + info.position,
    // Toast.LENGTH_SHORT).show();
    // return true;
    // }

    public class ImageAdapter extends BaseAdapter {
        private static final int ITEM_WIDTH = 136;

        private static final int ITEM_HEIGHT = 88;

        private final int mGalleryItemBackground;

        private final Context mContext;

        private final float mDensity;

        private Integer[] currentImageId = null;

        public ImageAdapter(Context c, Integer[] currentImageId) {
            mContext = c;
            this.currentImageId = currentImageId;
            // See res/values/attrs.xml for the <declare-styleable> that defines
            // Gallery1.
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();

            mDensity = c.getResources().getDisplayMetrics().density;
        }

        public int getCount() {
            return currentImageId.length;
        }

        public Object getItem(int position) {
            return currentImageId[position];
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                convertView = new ImageView(mContext);

                imageView = (ImageView) convertView;
                // imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                // imageView
                // .setLayoutParams(new Gallery.LayoutParams(
                // (int) (ITEM_WIDTH * mDensity + 0.5f),
                // (int) (ITEM_HEIGHT * mDensity + 0.5f)));

                // The preferred Gallery item background
                // imageView.setBackgroundResource(mGalleryItemBackground);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(currentImageId[position]);
            return imageView;
        }
    }
}
